package example.动态规划;

/**
 * 1、1、2、3、5、8、13、21、34·······
 */
public class Demo2 {


    public static int fib(int n) {
        if (n == 0) {
            return 0;
        }
        if (n == 1 || n == 2) {
            return 1;
        }
        return fib(n - 1) + fib(n - 2);
    }

    public static int fib3(int n) {
        if (n == 0) {
            return 0;
        }
        if (n == 1 || n == 2) {
            return 1;
        }
        int[] dp = new int[n];
        dp[0]=1;
        dp[1]=1;
        return fib3Help(n-1, dp);
    }

    public static int fib3Help(int n, int[] dp) {
        if (dp[n] != 0) {
            return dp[n];
        } else {
            dp[n] = fib3Help(n - 1,dp) + fib3Help(n - 2,dp);
            return dp[n];
        }
    }


    /**
     * 数学公式 arr[i]=arr[i-1]+arr[i-2]
     */
    public static int fib2(int n) {
        if (n == 0) {
            return 0;
        }
        if (n == 1 || n == 2) {
            return 1;
        }
        int[] ints = new int[n];
        ints[0] = 1;
        ints[1] = 1;
        for (int i = 2; i < n; i++) {
            ints[i] = ints[i - 1] + ints[i - 2];
        }
        return ints[n - 1];
    }


    public static void main(String[] args) {
        System.out.println(fib3(10));
        System.out.println(fib2(10));
    }
}
